
function gadget_selected() {
	alert('selected');
}

function load_gadget_info(gadget_id) {
	dojo.xhrGet({
	    url: 'http://localhost:3000/manage/load_gadget_info?id=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {gadget_info_loaded(data);},
		error: function (error) {gadget_info_loaded_error(error);}
	});
}

function gadget_info_loaded(data) {
	document.getElementById("gadget_info_pane").innerHTML = data;
	var divBody = dojo.byId('gadget_info_pane');
	dojo.parser.parse(divBody);
}

function gadget_info_loaded_error(error) {
	alert('ERROR gadget_info_loaded');
	alert(error.responseText);
}

function add_gadget() {
	alert('add');
}

function create_gadget() {
	alert('create');
}

function edit_gadget_spec(gadget_id) {
	dojo.xhrGet({
	    url: 'http://localhost:3000/manage/edit_gadget_spec?id=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {gadget_edit_loaded(data);},
		error: function (error) {gadget_edit_loaded_error(error);}
	});
}

function gadget_edit_loaded(data) {
	document.getElementById("gadget_info_pane").innerHTML = data;
	var divBody = dojo.byId('gadget_info_pane');
	dojo.parser.parse(divBody);
}

function gadget_edit_loaded_error(error) {
	alert('ERROR gadget_edit_loaded_error');
	alert(error.responseText);
}

function save_edit() {
	var gadget_id = document.getElementById('current_gadget').value;
	dojo.xhrPost({
	    url: 'http://localhost:3000/manage/save_gadget_spec?id=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {save_edit_complete(data);},
		error: function (error) {save_edit_error(error);}
	});
}

function cancel_edit() {
	var gadget_id = document.getElementById('current_gadget').value;
	load_gadget_info(gadget_id);
}

function save_edit_complete(data) {
	document.getElementById("gadget_info_pane").innerHTML = data;
	var divBody = dojo.byId('gadget_info_pane');
	dojo.parser.parse(divBody);	
}

function save_edit_error(error) {
	alert('ERROR save_edit_error');
	alert(error.responseText);
}

function publish_gadget(gadget_id) {
	dojo.xhrPost({
	    url: 'http://localhost:3000/manage/publish_gadget?id=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {publish_gadget_complete(data);},
		error: function (error) {publish_gadget_error(error);}
	});
}

function publish_gadget_complete(data) {
	load_gadget_info(data);
}

function publish_gadget_error(error) {
	alert('ERROR publish_gadget_error');
	alert(error.responseText);	
}

function unpublish_gadget(gadget_id) {
	dojo.xhrPost({
	    url: 'http://localhost:3000/manage/unpublish_gadget?id=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {unpublish_gadget_complete(data);},
		error: function (error) {unpublish_gadget_error(error);}
	});
}

function unpublish_gadget_complete(data) {
	load_gadget_info(data);
}

function unpublish_gadget_error(error) {
	alert('ERROR unpublish_gadget_error');
	alert(error.responseText);	
}

